package algotithm.leetcode.dichotomy.test1011;

/**
 * @author zhouyanxiang
 * @Date 2021-04-2021/4/8-22:39
 */
public class Solution {

    public static void main(String[] args) {
        int[] weights = {1,2,3,4,5,6,7,8,9,10};
        int D = 5;
        Solution solution = new Solution();
        int days = solution.shipWithinDays(weights, D);
        System.out.println(days);
    }

    public int shipWithinDays(int[] weights, int D) {
        int l = 0, r = Integer.MAX_VALUE;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (test(weights,D,mid)) {
                r = mid ;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    public boolean test(int[] nums,int D,int K) {
        int cur = K;
        for (int num : nums) {
            if (num > K) {
                return false;
            }
            if (cur < num) {
                cur = K;
                D--;
            }
            cur -= num;
        }
        return D > 0;
    }

}
